'''
Created on Apr 1, 2014

@author: sshadmin
'''
from lxml  import etree
from lxml import objectify
from collections import defaultdict
from model import Page, stop_words
from string import Template
from urllib2 import urlopen
from lxml.html import parse
from  gdata.youtube.service import YouTubeService
import json
import re
from datetime import datetime
from pprint import pprint
from mongoengine import *
import logging
import requests
from model import  server_url
from PIL import Image
import urllib
import PIL
import cStringIO
from StringIO import StringIO
from images import images as images_dict
from logger import getLogger
import traceback
log=getLogger("footyroom")
connect('pages')


class User(Document):
    meta = {'collection': 'footy_user'}
    creation_date = DateTimeField()
    modified_date = DateTimeField(default=datetime.now)
    device_id=StringField(primary_key=True)
    device_name=StringField()
    package_version=StringField()
    email=StringField()
    android_version=StringField()
    gcm_key=StringField()
    def save(self, *args, **kwargs):
        user=User.objects.filter(device_id=self.device_id).first()
        if not user:
            self.creation_date = datetime.now()
        else:
            self.creation_date=user.creation_date
        self.modified_date = datetime.now()
        return super(User, self).save(*args, **kwargs)
    def notify(self):
        print "user "+str(self.id)+"notified"
        
class PageLog(Document):
    meta = {'collection': 'footy_pagelog'}
    user=ReferenceField(User)
    page=StringField(unique_with='user')
    created = DateTimeField()    
class Feedback(Document):
    meta = {'collection': 'footy_feedback'}
    device_id=ReferenceField(User)
    feedback=StringField()
    created = DateTimeField()     
class MatchData(EmbeddedDocument):
    title = StringField()
    url = StringField()
    thumbnail=StringField()
    league=StringField()
    date=StringField()
    def to_dic(self):
        d=dict()
        d["title"]=self.title
        d['url']=self.url
        d['thumbnail']=self.thumbnail
        d['league']=self.league
        d['date']=self.date
        return d
    def __eq__(self, other) :
        if self.title==other.title and self.url==other.url:
            return True
        else:
            return False
    def __hash__(self):
        return hash(self.url)
class Main(Document):
    meta = {'collection': 'footy_main'}
    matches=ListField(EmbeddedDocumentField(MatchData))
    unique=StringField(primary_key=True)
    url="http://footyroom.com/"
    updated = DateTimeField()
    def fetch(self):
        try:
            self.unique='main'
            main=Main.objects.filter(unique='main').first()
            if main:
                self.matches=main.matches
                if (datetime.now()-main.updated).seconds<100:
                    self.matches=main.matches
                    return main.matches
            page = urlopen(self.url)
            root = etree.parse(page, etree.HTMLParser(encoding = "utf-8"))
            titles=root.xpath("//div/header/a/text()")
            thumbnails=root.xpath("//div/div/a/img/@src")
            urls=root.xpath("//div/header/a/@href")
            leagues=root.xpath("id('infinite-scroll')/div/div/footer/span/a/text()")
            dates=root.xpath("id('infinite-scroll')/div/div/footer/span/text()")
            fetched_matches=[]
            for i in range(len(titles)):
                title=titles[i]
                url="http://footyroom.com"+urls[i]
                thumbnail=thumbnails[i]
                league=leagues[i]
                date=dates[i].strip()
                fetched_matches.append(MatchData(title=title,url=url,thumbnail=thumbnail,league=league,date=date))
            self.matches=fetched_matches
            self.updated=datetime.now()
            self.save()
            return self.matches
        except:
            log.error(traceback.format_exc())
            log.error("old page returned")
            return self.matches
        def __eq__(self, other) : 
            if not other:
                return False
            return set(self.matches) == set(other.matches)
class Video(EmbeddedDocument):
    meta = {'allow_inheritance': True}
    pass
    def __hash__(self):
        return "video"
    def __eq__(self,other):
        return True
class DailyMotion(Video):
    video_id=StringField()
    thumbnail=StringField()
    url=StringField()
    type=StringField()
    def init(self):
        url_template=Template('http://www.dailymotion.com/embed/video/$video_id')
        self.url=url_template.safe_substitute(video_id=self.video_id)
        self.thumbnail=self.get_thumbnail()
        self.type='dailymotion'
    def get_thumbnail(self):
        dailymotion_api=Template("https://api.dailymotion.com/video/$video_id?fields=thumbnail_large_url").safe_substitute(video_id=self.video_id)
        try:
            response=urlopen(dailymotion_api)
            info=json.loads(response.read())
            return info['thumbnail_large_url']
        except:
            return ""
    def to_dic(self):
        d=dict()
        d['video_id']=self.video_id
        d['type']='dailymotion'
        d['thumbnail']=self.thumbnail
        d['url']=self.url
        return d
    def __hash__(self):
        return hash(self.url)
    def __eq__(self,other):
        return self.url == other.url
        
class Playwire(Video):
    video_id=StringField()
    publisher_id=StringField()
    thumbnail=StringField()
    url=StringField()
    type=StringField()
    thumbnail_compressed = FileField()
    def init(self):
        self.video_id=self.video_id
        self.publisher_id=self.publisher_id
        self.info=self.get_info()
        self.thumbnail=self.info['poster']
        response=requests.get(self.thumbnail)
#         file =cStringIO.StringIO(urllib.urlopen(self.thumbnail).read())
        img = Image.open(StringIO(response.content))
        img = img.resize((480,360), PIL.Image.ANTIALIAS)
        thumbnail_file = StringIO()
        img.save(thumbnail_file, 'JPEG',quality=40)
        thumbnail_file.seek(0)
        self.thumbnail_compressed.put(thumbnail_file,content_type = 'image/jpeg')
        self.thumbnail=server_url+"/get_image?id="+str(self.thumbnail_compressed.grid_id)
        self.url=self.info['src']
        self.type='playwire'
    def get_info(self):
#         config=Template('http://cdn.playwire.com/v2/$publisher_id/config/$video_id.json')
        config=Template('http://config.playwire.com/$publisher_id/videos/v2/$video_id/player.json')
        url=config.safe_substitute(publisher_id=self.publisher_id, video_id=self.video_id)
        p=urlopen(url)
        j=json.loads(p.read())
        p=p.read()
        p=urlopen(j['src'])
        root = objectify.fromstring(p.read())
        src=root.baseURL+"/"+root.media.attrib['url']
        j['src']=src
        return j
    def to_dic(self):
        d=dict()
        d['video_id']=self.video_id
        d['publisher_id']=self.publisher_id
        d['type']='playwire'
        d['thumbnail']=self.thumbnail
        d['url']=self.url
        return d
    def __eq__(self,other):
        return self.url == other.url
    def __hash__(self):
        return hash(self.url)  
    
class Team1Team2(EmbeddedDocument):
    team1 = StringField()
    team2 = StringField()
    def to_dic(self):
        d=dict()
        d['home']=self.team1
        d['away']=self.team2
        return d
class StatsItem(EmbeddedDocument):
    home = StringField()
    type = StringField()
    away = StringField()    
    def to_dic(self):
        d=dict()
        d['home']=self.home
        d['type']=self.type
        d['away']=self.away
        return d
class Article(EmbeddedDocument):
    title = StringField()
    thumbnail = StringField()
    snippet = StringField()
    url=StringField()
    def to_dic(self):
        d=dict()
        d['title']=self.title
        d['thumbnail']=self.thumbnail
        d['snippet']=self.snippet
        d['url']=self.url
        return d
        
class News(Document):
    meta = {'collection': 'footy_news'}
    articles=ListField(EmbeddedDocumentField(Article))
    updated = DateTimeField()
    unique=StringField(primary_key=True)
    def fetch(self):
        try:
            news=News.objects.filter(unique='news').first()
            if news :
                self.articles=news.articles
                if (datetime.now()-news.updated).seconds<300:
                    return news.articles
            self.unique='news'
            page = urlopen("http://footyroom.com/news-center/")
            root = etree.parse(page, etree.HTMLParser(encoding = "utf-8"))
            titles=root.xpath("id('mixnews')/div/div[2]/a/h2/text()")
            thumbnails=root.xpath("id('mixnews')/div/div[1]/a/img/@src")
            snippets=root.xpath("id('mixnews')/div/div[2]/div/p/text()")
            urls=root.xpath("id('mixnews')/div/div[1]/a/@href")
            fetched_articles=[]
            for i in range(len(titles)):
                try:
                    title=root.xpath("id('mixnews')/div["+str(i+1)+"]/div[2]/a/h2/text()")[0]
                    thumbnail=root.xpath("id('mixnews')/div["+str(i+1)+"]/div[1]/a/img/@src")[0]
                    snippet=root.xpath("id('mixnews')/div["+str(i+1)+"]/div[2]/div/p/text()")[0]
                    url=root.xpath("id('mixnews')/div["+str(i+1)+"]/div[1]/a/@href")[0]
                    a=Article(title=title,thumbnail=thumbnail,snippet=snippet,url=url)
                    fetched_articles.append(a)
                except:
                    log.error("article error")
                    log.error(traceback.format_exc())
            self.articles=fetched_articles
            self.updated=datetime.now()
            self.save()
        except:
            log.error(traceback.format_exc())
            log.error("old page returned")
            return news.articles
            
    def to_dic(self):
        return [x.to_dic() for x in self.articles]
    
    
class Preview(EmbeddedDocument):
    team1_thumb = StringField()
    team2_thumb = StringField()
    team1 = StringField()
    team2 = StringField()
    def to_dic(self):
        d=dict()
        d['team1']=self.team1
        d['team2']=self.team2
        d['team1_thumb']=self.team1_thumb
        d['team2_thumb']=self.team2_thumb
        return d
        
class Previews(Document):
    meta = {'collection': 'footy_previews'}
    game_previews=ListField(EmbeddedDocumentField(Preview))
    updated = DateTimeField()
    unique=StringField(primary_key=True)
    def fetch(self):
        games=Previews.objects.filter(unique='previews').first()
        if games!=None and (datetime.now()-games.updated).seconds<300:
            self.game_previews=games.game_previews
            return games.game_previews
        self.unique='previews'
        page = urlopen("http://footyroom.com/")
        root = etree.parse(page, etree.HTMLParser(encoding = "utf-8"))
        teams=root.xpath("id('previews')/div[2]/div/div/div/div/div/a[2]/div/text()")
        thumbnails=root.xpath("id('previews')/div[2]/div/div/div/div/div/a/div/div/img/@src")
        fetched_previews=[]
        for i in range(0,len(teams),2):
            a=Preview(team1=str(teams[i]),team2=str(teams[i+1]),team1_thumb="http://footyroom.com"+str(thumbnails[i]),team2_thumb="http://footyroom.com"+str(thumbnails[i+1]))
            fetched_previews.append(a)
        self.game_previews=fetched_previews
        self.updated=datetime.now()
        self.save()
    def to_dic(self):
        return [x.to_dic() for x in self.game_previews]
                
class Match(Document):
    meta = {'collection': 'footy_match'}
    url = StringField(primary_key=True)
    venue = StringField()
    stage=StringField()
    referee=StringField()
    league=StringField()
    status=StringField()
    spectators=StringField()
    season=StringField()
    round=StringField()
    region=StringField()
    match_time=StringField()
    teams=EmbeddedDocumentField(Team1Team2)
    score=EmbeddedDocumentField(Team1Team2)
    score_halftime=EmbeddedDocumentField(Team1Team2)
    thumbnails=EmbeddedDocumentField(Team1Team2)
    stats=ListField(EmbeddedDocumentField(StatsItem))
    details=ListField()
    video=EmbeddedDocumentField(Playwire)
    updated = DateTimeField()
    def fetch(self):
        try:
            match=Match.objects.filter(url=self.url).first()
            if match:
                self.teams=match.teams
                self.score=match.score
                self.score_halftime=match.score_halftime
                self.thumbnails=match.thumbnails
                self.stats=match.stats
                self.details=match.details
                self.video=match.video
                self.league=match.league
                self.referee=match.referee
                self.status=match.status
                self.match_time=match.match_time
                self.stage=match.stage
                self.venue=match.venue
                self.region=match.region
                self.spectators=match.spectators
                self.season=match.season
                self.round=match.round
                if(datetime.now()-match.updated).seconds<100:
                    return match
            page = urlopen(self.url)
            root = etree.parse(page, etree.HTMLParser(encoding = "utf-8"))
            script=root.xpath("//div[@class='video-section clearfix']//script/text()")[0]
            print "script "+str(script)
            video_type=""
            if "playwire" in script:
                script=script.replace("\/","/")
                l=script.split("http://config.playwire.com/")[1]
                l=l.split("/videos/v2/")
                publisher_id=l[0]
                video_id=l[1].split("/player.json")[0]
                video_type="playwire"
            elif "dailymotion" in script:
                l=script.split('dailymotion.com/swf/')[1]
                l=l.split('"}])')
                video_id=l[0]
                video_type="dailymotion"
            if match and match.video.video_id==video_id:
                print "video exists"
                p=match.video
            else:
                print "video don't exist"
                if video_type=="playwire":
                    p=Playwire(video_id=video_id,publisher_id=publisher_id)
                    p.init()
                elif video_type=="dailymotion":
                    p=DailyMotion(video_id=video_id)
                    p.init()
            self.video=p
            logging.info(self.video.to_dic())
#             info=root.xpath("//script/text()")[8]
#             info=info.split("DataStore['match'] =")[1]
#             info=json.loads(info)
            info={}
            stats_dict=dict()
            stats=[]
            if 'stats' in info:
                for item in info['stats']:
                    if item['type'] not in stats_dict:
                        if item['side']=='home':
                            i=StatsItem(home=str(item['value']),away=None,type=item['type'])
                        elif item['side']=='away':
                            i=StatsItem(home=None,away=str(item['value']),type=item['type'])  
                        stats_dict[item['type']]=i
                        stats.append(i)
                    else:
                        i=stats_dict[item['type']]
                        if i.home==None:
                            i.home=str(item['value'])
                        elif i.away==None:
                            i.away=str(item['value'])
            else:
                stats=[]
            for item in stats:
                if item.home==None:
                    item.home="0"
                if item.away==None:
                    item.away="0"
            self.stats=stats
#             score1=info["homeScore"]
#             score2=info["awayScore"]
#             score1_halftime=info["homeScoreHT"]
#             score2_halftime=info["awayScoreHT"]
#             self.score_halftime=Team1Team2(team1=score1_halftime,team2=score2_halftime)
#             venue=info['venueName']
#             self.venue=venue
#             stage=info['stage']
#             self.stage=stage
#             if 'referee' in info:
#                 self.referee=info['referee']
#             self.league=info['league']
#             self.region=info['region']
#             self.season=info['season']
#             self.round=info['round']
#             if 'spectators' in info:
#                 self.spectators=str(info['spectators'])
#             self.match_time=str(info['datetime']['sec'])
#             self.status=info['statusType']
#             team1=info['homeTeam']['name']
#             team2=info['awayTeam']['name']
#             self.teams=Team1Team2(team1=team1,team2=team2)
#             self.score=Team1Team2(team1=score1,team2=score2)
    #         for item in stats:
    #             if item['type'] in {'fouls','shots-off-target','corners','possesion','shots-on-target','yellow-cards','red-cards','crosses','counter-attacks'}:
    #                 self.stats.append(StatsItem(side=item['side'],type=item['type'],value=str(item['value'])))  
            thumbnail1="http://footyroom.com"+root.xpath("id('mainBox')/div[1]/div[1]/div[1]/img[1]/@src")[0]
            thumbnail2="http://footyroom.com"+root.xpath("id('mainBox')/div[1]/div[1]/div[1]/img[2]/@src")[0]
            self.thumbnails=Team1Team2(team1=thumbnail1,team2=thumbnail2)
            self.updated = datetime.now()
            self.save()
        except:
            log.error("old page returned")
            log.error(traceback.format_exc())
            
    def to_dic(self):
        d=dict()
        d['video']=self.video.to_dic()
#         d['region']=self.region
#         d['spectators']=self.spectators
        d['stats']=[x.to_dic() for x in self.stats]
#         d['score']=self.score.to_dic()
#         d['score_halftime']=self.score_halftime.to_dic()
        d['thumbnails']=self.thumbnails.to_dic()
#         d['teams']=self.teams.to_dic()
#         d['league']=self.league
#         d['referee']=self.referee
#         d['status']=self.status
#         d['match_time']=self.match_time
#         d['stage']=self.stage
#         d['venue']=self.venue
#         d['season']=self.season
#         d['round']=self.round
        return d
# m=Previews()
# m.fetch()
# print x.to_dic()        
# m=Match("http://footyroom.com/ac-milan-4-2-real-madrid-2014-12/")
# m.fetch()
# m=News()
# m.fetch()
# print m.to_dic()
# m=Main()
# main=m.fetch()
# for match in main:
#     print match.to_dic()
#     x=Match(match.url)
#     x.fetch()
#     print x.to_dic()